package com.danger.Util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;

/**
 * Created by 俊 on 2016/6/10.
 */
public class ImageHelper {

    /**
     * 根据传入的图片、色相、饱和度、亮度，返回处理之后的图片(RGB)
     *
     * @param bm
     * @param hue
     * @param saturation
     * @param lum
     * @return
     */
    public static Bitmap handleImageEffect(Bitmap bm, float hue, float saturation, float lum) {
        //传入的Bitmap是默认不可修改的，因此需重新创建一个Bitmap
        Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
        //创建与原图大小相同的画布，之后的所有操作都在该画布上实现
        Canvas canvas = new Canvas(bitmap);
        //设置画笔，并指定抗锯齿
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //色相Matric
        ColorMatrix hueMatrix = new ColorMatrix();
        //修改red
        hueMatrix.setRotate(0, hue);
        //修改green
        hueMatrix.setRotate(1, hue);
        //修改blue
        hueMatrix.setRotate(2, hue);
        //饱和度Matric
        ColorMatrix saturationMatric = new ColorMatrix();
        saturationMatric.setSaturation(saturation);
        //亮度Matric
        ColorMatrix lumMatric = new ColorMatrix();
        lumMatric.setScale(lum, lum, lum, 1);

        //将三种Matric进行融合
        ColorMatrix imageMatric = new ColorMatrix();
        imageMatric.postConcat(hueMatrix);
        imageMatric.postConcat(saturationMatric);
        imageMatric.postConcat(lumMatric);

        paint.setColorFilter(new ColorMatrixColorFilter(imageMatric));
        canvas.drawBitmap(bm, 0, 0, paint);
        return bitmap;
    }

    /**
     * 像素点操作图片,底片效果
     *
     * @param bm
     * @return
     */
    public static Bitmap handleImageNegative(Bitmap bm) {

        int width = bm.getWidth();
        int height = bm.getHeight();
        int color;
        int r, g, b, a;

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        int[] oldPixels = new int[width * height];
        int[] newPixels = new int[width * height];

        bm.getPixels(oldPixels, 0, width, 0, 0, width, height);
        for (int i = 0; i < width * height; i++) {
            color = oldPixels[i];
            r = Color.red(color);
            g = Color.green(color);
            b = Color.blue(color);
            a = Color.alpha(color);

            r = 255 - r;
            g = 255 - g;
            b = 255 - b;

            if (r > 255) {
                r = 255;
            } else if (r < 0) {
                r = 0;
            }

            if (g > 255) {
                g = 255;
            } else if (g < 0) {
                g = 0;
            }

            if (b > 255) {
                b = 255;
            } else if (b < 0) {
                b = 0;
            }
            newPixels[i] = Color.argb(a, r, g, b);
        }
        bitmap.setPixels(newPixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

    /**
     * 像素点操作图片,怀旧效果
     *
     * @param bm
     * @return
     */
    public static Bitmap handleImageOldphoto(Bitmap bm) {

        int width = bm.getWidth();
        int height = bm.getHeight();
        int color;
        int r, g, b, a;
        int r1, b1, g1;

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        int[] oldPixels = new int[width * height];
        int[] newPixels = new int[width * height];

        bm.getPixels(oldPixels, 0, width, 0, 0, width, height);
        for (int i = 0; i < width * height; i++) {
            color = oldPixels[i];
            r = Color.red(color);
            g = Color.green(color);
            b = Color.blue(color);
            a = Color.alpha(color);

            r1 = (int) (0.393 * r + 0.769 * g + 0.189 * b);
            g1 = (int) (0.349 * r + 0.686 * g + 0.168 * b);
            b1 = (int) (0.272 * r + 0.534 * g + 0.131 * b);

            if (r1 > 255) {
                r1 = 255;
            } else if (r1 < 0) {
                r1 = 0;
            }

            if (g1 > 255) {
                g1 = 255;
            } else if (g1 < 0) {
                g1 = 0;
            }

            if (b1 > 255) {
                b1 = 255;
            } else if (b1 < 0) {
                b1 = 0;
            }
            newPixels[i] = Color.argb(a, r1, g1, b1);
        }
        bitmap.setPixels(newPixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

    /**
     * 像素点操作图片,浮雕效果
     *
     * @param bm
     * @return
     */
    public static Bitmap handleImageRelief(Bitmap bm) {

        int width = bm.getWidth();
        int height = bm.getHeight();
        int color, colorBefore;
        int r, g, b, a;
        int r1, b1, g1;

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        int[] oldPixels = new int[width * height];
        int[] newPixels = new int[width * height];

        bm.getPixels(oldPixels, 0, width, 0, 0, width, height);
        for (int i = 1; i < width * height; i++) {
            colorBefore = oldPixels[i - 1];
            r = Color.red(colorBefore);
            g = Color.green(colorBefore);
            b = Color.blue(colorBefore);
            a = Color.alpha(colorBefore);

            color = oldPixels[i];
            r1 = Color.red(color);
            g1 = Color.green(color);
            b1 = Color.blue(color);

            r = r - r1 + 127;
            g = g - g1 + 127;
            b = b - b1 + 127;

            if (r > 255) {
                r = 255;
            } else if (r < 0) {
                r = 0;
            }

            if (g > 255) {
                g = 255;
            } else if (g < 0) {
                g = 0;
            }

            if (b > 255) {
                b = 255;
            } else if (b < 0) {
                b = 0;
            }
            newPixels[i] = Color.argb(a, r, g, b);
        }
        bitmap.setPixels(newPixels, 0, width, 0, 0, width, height);
        return bitmap;
    }
}
